package com.lhd.qd.module.system.employee.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhd.qd.common.base.ApiResult;
import com.lhd.qd.common.base.BaseController;
import com.lhd.qd.common.base.BasePageQuery;
import com.lhd.qd.module.system.employee.model.dto.EmployeePageQuery;
import com.lhd.qd.module.system.employee.model.dto.EmployeeSaveDTO;
import com.lhd.qd.module.system.employee.model.vo.EmployeeDetailVO;
import com.lhd.qd.module.system.employee.model.vo.EmployeeListVO;
import com.lhd.qd.module.system.employee.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "员工")
@RequestMapping("employee")
@RestController
public class EmployeeController extends BaseController {

    @Autowired
    private EmployeeService employeeService;

    @ApiOperation(value = "分页列表", response = EmployeeListVO.class)
    @GetMapping
    public ResponseEntity<ApiResult<IPage<EmployeeListVO>>> pageEmployee(EmployeePageQuery query) {

        return success(employeeService.pageEmployee(query));
    }

    @ApiOperation(value = "详情", response = EmployeeDetailVO.class)
    @GetMapping("{id}")
    public ResponseEntity<ApiResult<EmployeeDetailVO>> getEmployeeById(@PathVariable("id") Long id) {

        return success(employeeService.getEmployeeById(id));
    }

    @ApiOperation(value = "新增")
    @PostMapping
    public ResponseEntity<ApiResult> saveEmployee(@Validated @RequestBody EmployeeSaveDTO saveDTO) {

        return successOrFail(employeeService.saveEmployee(saveDTO));
    }

    @ApiOperation(value = "修改")
    @PutMapping("{id}")
    public ResponseEntity<ApiResult> updateEmployeeById(@PathVariable("id") Long id, @Validated @RequestBody EmployeeSaveDTO saveDTO) {

        return successOrFail(employeeService.updateEmployeeById(id, saveDTO));
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("{id}")
    public ResponseEntity<ApiResult> removeEmployeeById(@PathVariable("id") Long id) {

        return successOrFail(employeeService.removeEmployeeById(id));
    }
}
